/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eu.games.esmi.ieat.indicatorvalues;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Tree;
import eu.games.esmi.ieat.indicatorvalues.data.EMF;
import eu.games.esmi.ieat.indicatorvalues.data.GPIValues;
import eu.games.esmi.ieat.indicatorvalues.data.IndicatorSeries;
import it.polimi.games.epkb.controller.GpiKpiJpaController;
import it.polimi.games.epkb.controller.StoredValuesJpaController;
import it.polimi.games.epkb.entityClasses.GpiKpi;
import it.polimi.games.epkb.entityClasses.StoredValues;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author user
 */
public class SystemAnalysisTab extends HorizontalLayout implements Tree.ValueChangeListener {

    IndicatorChart indicatorchart = new IndicatorChart("System Analysis", null);

    public SystemAnalysisTab() {

        super();
        init();

    }

    public void init() {

        this.setMargin(true);
        this.setSizeFull();

        Tree tree = new IndicatorTreeGenerator().createTree(IndicatorTreeGenerator.SYSTEM_TREE);
        tree.addListener(this);



        this.addComponent(tree);
        this.setComponentAlignment(tree, Alignment.TOP_LEFT);

        this.addComponent(indicatorchart);
        this.setComponentAlignment(indicatorchart, Alignment.TOP_LEFT);
        this.setExpandRatio(indicatorchart, 1.0f);
    }

    public void valueChange(ValueChangeEvent event) {

        Tree t = (Tree) event.getProperty();

        EntityManagerFactory emf = Persistence.createEntityManagerFactory(EMF.PU_NAME);

        GpiKpiJpaController indicatorsCtrl = new GpiKpiJpaController(emf);
        StoredValuesJpaController valuesCtrl = new StoredValuesJpaController(emf);

        LinkedList<IndicatorSeries> series = new LinkedList<IndicatorSeries>();

        Set<String> selezione = (Set<String>) t.getValue();

        for (String selectedItem : selezione) {

            if (selectedItem.contains("-")) {
                String component = selectedItem.substring(0, selectedItem.indexOf("-"));

                String indicator = selectedItem.substring(selectedItem.indexOf("-") + 1);

                GpiKpi gpikpi = indicatorsCtrl.findGpiKpi(indicator);

                if (gpikpi != null) {

                    IndicatorSeries is = new IndicatorSeries(gpikpi.getName() + "(" + component + ")", null, "UM");

                    EntityManager em = emf.createEntityManager();


                    //Query q = em.createQuery("SELECT s from StoredValues s ORDER BY s.definitionTime WHERE s.idComponent = ?1 and s.idGpiKpi = ?2 LIMIT 0, 10000");
                    Query q = em.createQuery("SELECT s from StoredValues s ORDER BY s.definitionTime DESC");
                    q.setMaxResults(10000);
                    //q.setParameter(1, component);
                    //q.setParameter(2, gpikpi);
                    List<StoredValues> result = q.getResultList();


                    is.setValues(GPIValues.getValues(result, component, indicator));

                    is.setStatus(GPIValues.getStatus(result, component, indicator));

                    is.setNrl(GPIValues.getNrl(component, indicator));
                    is.setNrh(GPIValues.getNrh(component, indicator));
                    is.setErl(GPIValues.getErl(component, indicator));
                    is.setErh(GPIValues.getErh(component, indicator));

                    series.add(is);

                    em.close();

                }
                indicatorchart.setSeries(series);
            }
        }


        emf.close();

    }
}
